package com.ys.controller.api.hr;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.ys.service.flow.YspTaskService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ys.config.MenuId;
import com.ys.entity.hr.TwlStockIn;
import com.ys.entity.hr.TwlStockInDetail;
import com.ys.entity.sys.YsCol;
import com.ys.enume.sys.YsColEnum;
import com.ys.service.hr.TwlStockInService;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.UploadService.UploadDto;
import com.ys.util.ExportExcelUtils;
import com.ys.util.shiro.ShiroUtils;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;

@Controller
@RequestMapping("/api/inter/twlStockIn")
public class ApiInterTwlStockInController {
	
//	private static final Logger logger = Logger.getLogger(ApiAdminLoginController.class);
	
	@Autowired
	private CommonApiService<TwlStockIn> commonApiService;
	@Autowired
	private CommonApiService<TwlStockInDetail> commonApiServiceDetail;
	@Autowired
	private TwlStockInService twlStockInService;

	@Autowired
	private YspTaskService yspTaskService;
	
	@RequiresPermissions("twlStockIn:view")
	@RequestMapping("/search")
    @ResponseBody
	public LayuiTableVoJA search(@ModelAttribute TwlStockInDetail cond){
		
		cond = setSeachSql(cond);
		return commonApiServiceDetail.searchLay(cond);
	}
	/**
	 * 新增
	 */
	@RequestMapping("/add")
	@ResponseBody
	@RequiresPermissions("twlStockIn:add")
	public R add(String bid, String companyVid, String orgid, String remarkStockIn, String subData) {
		try {
			//修改
			return twlStockInService.add(bid, companyVid, orgid, remarkStockIn, subData, MenuId.TWL_StockIn);
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
		}
	}

	/**
	 * 编辑
	 */
	@RequiresPermissions("twlStockIn:add")
	@PostMapping("/edit")
    @ResponseBody
	public R edit(String bid, String companyVid, String orgid, String remarkStockIn, String subData){
		try {
			return twlStockInService.edit(bid, companyVid, orgid, remarkStockIn, subData, MenuId.TWL_StockIn);
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
		}
	}
	
	/**
	 * 流转通过
	 */
	@RequiresPermissions("twlStockIn:add")
	@PostMapping("/flowPass")
    @ResponseBody
	public R flowPass(HttpServletRequest request, Long id){
		TwlStockIn twlStockIn = new TwlStockIn().setId(id).queryFirst();
		try {
			return twlStockInService.flowPass(twlStockIn.getBid(), ShiroUtils.getUserId());
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("服务器正在开小差，请联系客服");
		}
	}
	
	/**
	 * 流转驳回
	 */
	@RequiresPermissions("twlStockIn:add")
	@PostMapping("/flowRefuse")
    @ResponseBody
	public R flowRefuse(HttpServletRequest request, Long id, String reason){
		TwlStockIn twlStockIn = new TwlStockIn().setId(id).queryFirst();
		try {
			return twlStockInService.flowRefuse(twlStockIn.getBid(), reason, ShiroUtils.getUserId());
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("服务器正在开小差，请联系客服");
		}
	}


	/**
	 * 流转驳回
	 */
	@RequiresPermissions("twlStockIn:add")
	@PostMapping("/flowInit")
	@ResponseBody
	public R flowInit(HttpServletRequest request, Long id, String reason){
		TwlStockIn twlStockIn = new TwlStockIn().setId(id).queryFirst();
		try {
			return yspTaskService.flowInit(twlStockIn.getBid(), reason, ShiroUtils.getUserId());
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("服务器正在开小差，请联系客服");
		}
	}

	
	/**
	 * 删除
	 * @throws Exception 
	 */
	@PostMapping("/delete")
    @ResponseBody
	public R delete(Long id){
		
		try {
			return twlStockInService.deleteTrue(id);
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("操作失败，请联系客服");
		}
	}
	
	/*
	 * 导出
	 */
	@RequiresPermissions("twlStockIn:export")
	@RequestMapping("/export")
    @ResponseBody
    public R export(@ModelAttribute TwlStockInDetail cond) throws Exception{
		
		cond = setSeachSql(cond);
		cond.setOrderby("A.Id DESC");
		
		cond.setPage(0).setSize(999999);

		List<Map<String, Object>> listData = cond.queryMap();
        
    	List<YsCol> listField = new YsCol()
    			.setMenuId(MenuId.TWL_StockIn)
    			.setFrontShow(YsColEnum.FrontShow.SHOW.value)
    			.setOrderby("A.SortNo asc")
    			.queryList();
    	
    	
        ExportExcelUtils<TwlStockIn> util = new ExportExcelUtils<TwlStockIn>(new TwlStockIn());
        
        String toFilePath = util.exportExcel(listData, listField, "物料入库单");
        UploadDto dto = commonApiService.uploadLocalToServer(toFilePath);
        return R.ok(dto.getUrl());
    }

    /**
     * 设置查询的cond的sql设置
     * @param cond
     * @return
     */
    public TwlStockInDetail setSeachSql(TwlStockInDetail cond){
    	
    	cond.addLeftJoin("TWL_StockIn B ON B.Bid = A.BidStockIn")
    		.addField("B.Bid bid")
    		.addField("B.EffectStock effectStock")
    		.addField("B.Remark remarkStockIn");
    	
    	cond.addLeftJoin("TB_CompanyInfo B1 on B1.Vid = B.CompanyVid")
			.addField("B1.Vid companyVid")
    		.addField("B1.Vname companyVname");
    	
    	cond.addLeftJoin("TS_OrganizationInfo B2 on B2.Orgid = B.Orgid")
			.addField("B2.Orgid orgid")
    		.addField("B2.OrgName orgName")
    		.addField("B2.HeadUserId orgHeadUserId");
    	
    	cond.addLeftJoin("YS_ProcInst C ON C.Bid = B.Bid")
    		.addField("C.UpdateTime auditTime")
    		.addField("C.CreateUserId createUserId");
    	
    	cond.addLeftJoin("TB_Material D ON D.Code = A.MaterialCode")
    		.addField("D.Name materialName")
    		.addField("D.Remark materialRemark");
    	
    	
		
        return  cond;
    }
}
